// pages/tabBar/cate/cate.js
import {
  Config
} from "../../../utils/config.js";
import api from "../../../utils/api.js"
import * as com from "../../../utils/common.js"
const Base = new com.Base();
var app = getApp();

Page({
  data: {
    navList: [{
        id: 1,
        name: '推荐专区'
      },
      {
        id: 2,
        name: '爆品区'
      },
      {
        id: 3,
        name: 'aaa'
      },
      {
        id: 4,
        name: 'aaa'
      },
    ],

    categoryList: [],

    currentCategory: {
      id: 1,
      wap_banner_url: '/images/01.jpg',
      front_name: 'asasdad',
      name: '123123',
      subCategoryList: [{
          wap_banner_url: '/images/goods.jpg',
          name: '12312'
        },
        {
          wap_banner_url: '/images/goods.jpg',
          name: '12312'
        },
        {
          wap_banner_url: '/images/goods.jpg',
          name: '12312'
        },
        {
          wap_banner_url: '/images/goods.jpg',
          name: '12312'
        },
      ]
    },

    scrollLeft: 0,

    scrollTop: 0,

    goodsCount: 0,

    scrollHeight: 0
  },

  onLoad: function(options) {
    // this.getCatalog();
  },

  getCatalog: function() {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.CatalogList).then(function(res) {
      that.setData({
        navList: res.data.categoryList,
        currentCategory: res.data.currentCategory
      });
      wx.hideLoading();
    });
    util.request(api.GoodsCount).then(function(res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });

  },

  getCurrentCategory: function(id) {
    let that = this;
    util.request(api.CatalogCurrent, {
        id: id
      })
      .then(function(res) {
        that.setData({
          currentCategory: res.data.currentCategory
        });
      });
  },

  getList: function() {
    var that = this;
    util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.cat_id)
      .then(function(res) {
        that.setData({
          categoryList: res.data,
        });
      });
  },

  switchCate: function(event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }

    this.getCurrentCategory(event.currentTarget.dataset.id);
  }
})